/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webproxy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.Hashtable;

/**
 *
 * @author MinhVu
 */
public class MyCache
{    
    private  String basePath;
    private long MinFreeSpace;// 
    private File fdcache;
    private Hashtable htData ;  
   
    //<editor-fold defaultstate="colldapsed" desc="GET - SET">
    //GET
    public String getBasePath()
    {
        return basePath;
    }
    public long getMinFreeSpace()
    {
        return MinFreeSpace;
    }
    public Hashtable getHtDatas()
    {
        return htData;
    }
    //SET
    public void setBasePath(String bspath)
    {
        basePath = bspath;
    }
    public void setMinFreeSpace(long mfSpace)
    {
        MinFreeSpace = mfSpace;
    }
    public void setHtData(Hashtable hstable)
    {
        htData = hstable;
    }
    //</editor-fold>
    
    public MyCache()
    {
          basePath =System.getProperty("user.dir") +File.separatorChar + "Cache";
          MinFreeSpace = 0;
          htData = new Hashtable(); 
         
    }
    public void Init()//viet choi cho vui
    {
        fdcache = new File(basePath);   
        if(fdcache.isDirectory())
            for(File fi : fdcache.listFiles())
            {
                File fKey = new File(basePath+File.separatorChar +fi.getName());
                 for(File fj : fKey.listFiles())
                     htData.put(fj.getName(),fi.getName());
            }            
    }
    public void CreateDir()
    {
        try { 
            fdcache = new File(basePath);   
            if(!fdcache.isDirectory())
                fdcache.mkdir();
            else
            {
            }
        } catch (Exception e) {
        }
        
    }
    public FileInputStream DocFile(String rawUrl)
    {
        FileInputStream in = null;
        try
        {
            htData.put(rawUrl,new Date());                   
            in = new FileInputStream(rawUrl);
        }
        catch(FileNotFoundException fnf)
        {
            try
            {
                    System.out.println("File Not Found:"+rawUrl+" "+fnf);
            }
            catch (Exception e) 
            {}
        }
        finally
        {
                return in;
        }
    }
    public boolean IsCached(String rawUrl)
    {
        // Generate filename from URL
        String filename = CreateFileName(rawUrl);
        if (filename == null)
                return false;
//        if(htData.isEmpty())
//            return false;
//        if(htData.containsKey(filename))
//            return true;
        //Kiem tra truc tiep trong thu muc
        fdcache = new File(basePath+File.separatorChar+filename);
        if(fdcache.exists())
            return true;
        return false;
    }
    
    @SuppressWarnings("UseOfObsoleteCollectionType")
    
    public String CreateFileName(String rawUrl)
    {
        String filename =rawUrl.replace('/','@') ;
        return filename;
    }
}
